package com.irdstudio.efp.esb.service.impl.hj;


import com.irdstudio.basic.framework.core.constant.ESBConstant;
import com.irdstudio.basic.framework.core.exception.BizException;
import com.irdstudio.basic.framework.core.util.SpringContextUtils;
import com.irdstudio.basic.framework.core.util.StringUtil;
import com.irdstudio.basic.framework.core.util.TraceUtil;
import com.irdstudio.efp.esb.common.client.resp.EsbRespServiceBean;
import com.irdstudio.efp.esb.common.constant.ESBClientConstance;
import com.irdstudio.efp.esb.common.constant.EsbBizEnums;
import com.irdstudio.efp.esb.service.bo.req.hj.ReqCusInfoCreBean;
import com.irdstudio.efp.esb.service.bo.resp.hj.RespCusInfoCreBean;
import com.irdstudio.efp.esb.service.client.ESBBeanCreator;
import com.irdstudio.efp.esb.service.client.ESBClientFactory;
import com.irdstudio.efp.esb.service.client.ESBRequestBuilder;
import com.irdstudio.efp.esb.service.client.EsbBaseBean;
import com.irdstudio.efp.esb.service.client.impl.BDESBBeanCreator;
import com.irdstudio.efp.esb.service.client.impl.WSDESBBeanCreator;
import com.irdstudio.efp.esb.service.client.impl.YEDESBBeanCreator;
import com.irdstudio.efp.esb.service.facade.hj.CustomerInfoCreateService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

/**
 * 用户及客户信息建立接口实现类
 *
 * @author qiuyf
 * @since 2018年12月20日 下午7:42:50
 * @version 1.0
 */
@Service("customerInfoCreateService")
public class CustomerInfoCreateServiceImpl implements CustomerInfoCreateService {

	Logger logger = LoggerFactory.getLogger(CustomerInfoCreateServiceImpl.class);

	/**
	 * @description:用户及客户信息建立
	 * @param reqCusInfoCreBean
	 * @return
	 * @date:2018年12月21日 下午3:26:09
	 * @author:wangyk3
	 */
	@Override
	public RespCusInfoCreBean customerInfoCreate(ReqCusInfoCreBean reqCusInfoCreBean) throws Exception {
		// 客户建档，机构id必填
		TraceUtil.setInstId(reqCusInfoCreBean.getInstId());

		// 返回对象
		RespCusInfoCreBean respCusInfoCreBean = null;
		EsbRespServiceBean  esbRespServiceBean = null;
		logger.info("根据证件号码：" + reqCusInfoCreBean.getCertNo() + "前往互金建立客户信息开始................");
		try {
			//因为优e贷的Instid不是固定的，所以放在前面当产品id是优e贷的时候就直接进入优e贷
			if (ESBClientConstance.YED_PROD_TYPE.equals(reqCusInfoCreBean.getProdType())){
				esbRespServiceBean = ESBClientFactory.buildClient().withBody(reqCusInfoCreBean)
						.withTradeNo(ESBConstant.TRADE_CODE_20120001).withScene(ESBConstant.SECNE_NO_01)
						.withESBBeanFactory((YEDESBBeanCreator) SpringContextUtils.getBean("YEDESBBeanCreator"))
						.create().sendAndReceive();
			} else if(ESBClientConstance.bdInstId.equals(reqCusInfoCreBean.getInstId())){
				// 1.发送请求信息--> ESB
				esbRespServiceBean = ESBClientFactory.buildClient().withBody(reqCusInfoCreBean)
						.withTradeNo("20120001").withScene("01")
						.withESBBeanFactory((BDESBBeanCreator) SpringContextUtils.getBean("BDESBBeanCreator"))
						.create().sendAndReceive();
			}else if (ESBClientConstance.wsdInstId.equals(reqCusInfoCreBean.getInstId())){
				esbRespServiceBean = ESBClientFactory.buildClient().withBody(reqCusInfoCreBean)
						.withTradeNo("20120001").withScene("01")
						.withESBBeanFactory((WSDESBBeanCreator) SpringContextUtils.getBean("WSDESBBeanCreator"))
						.create().sendAndReceive();
			} else if (ESBClientConstance.psdCreditInstId.equals(reqCusInfoCreBean.getInstId())){
				reqCusInfoCreBean.setProdType("10001002");
				EsbBaseBean esbBaseBean = EsbBaseBean.EsbBaseBeanBuilder.
						anEsbBaseBean().
						withPLAFMUSRID(reqCusInfoCreBean.getChannlNo()+reqCusInfoCreBean.getCertNo()).
						withPLATFMID("E00004").
						withINSTID("0052").withGLBLSRLNO(reqCusInfoCreBean.getGlobaNo()).build();
				esbRespServiceBean = ESBRequestBuilder.buildClient().
						withEsbBaseBean(esbBaseBean)
						.withBody(reqCusInfoCreBean)
						.withTradeNo(ESBConstant.TRADE_CODE_20120001)
						.withScene(ESBConstant.SCENE_NO_01)
						.create()
						.sendAndReceive();
			}else {
				// 1.发送请求信息--> ESB
				esbRespServiceBean = ESBClientFactory.buildClient().withBody(reqCusInfoCreBean)
						.withTradeNo("20120001").withScene("01")
						.create().sendAndReceive();
			}


			if(EsbBizEnums.RetCodeEnum.SUCCESS.VALUE.equals(esbRespServiceBean.getSysHead().getRetInfArry().get(0).getRetCd())) {
				respCusInfoCreBean = esbRespServiceBean.getBody(RespCusInfoCreBean.class);
				// 2.1  判断是否有正常客户号返回
				if (StringUtil.isEmpty(respCusInfoCreBean.getClntNo())) { // 若为空
					throw new BizException("调用用户及客户信息建立接口异常，返回客户号为空！");
				}
				// 2.2 判断是否有正常用户号返回
				if (StringUtil.isEmpty(respCusInfoCreBean.getUsrID())) {  // 若为空
					throw new BizException("调用用户及客户信息建立接口异常，返回用户号为空！");
				}
			} else {
				throw new BizException(esbRespServiceBean.getSysHead().getRetInfArry().get(0).getRetMsg());
			}

		}catch (Exception e) {
			logger.error(e.getMessage());
			throw e;
		}
		logger.info("根据证件号码：" + reqCusInfoCreBean.getCertNo() + "前往互金建立客户信息结束!");
		return respCusInfoCreBean;
	}
}
